import 'package:flutter/material.dart';

import '../../../../assets/const.dart';

const Color _defaultBotMsgColor = Color(0xFFF6F7F9);
const Color _defaultUserMsgColor = Color(0xFFA56DFF);

class BaseAskMsgCard extends StatefulWidget {
  final Widget? child;
  final int? role; // 0:自己，1：luca
  final Color? botMsgBgColor;
  final Color? userMsgBgColor;
  final Decoration? decoration;
  final bool? isSupportChoose; // 是否支持可选择, 默认false
  final bool? messageSelected; // 是否选中
  final GestureTapCallback? onTapChoose; // 勾选消息
  const BaseAskMsgCard({
    super.key,
    required this.role,
    this.child,
    this.botMsgBgColor,
    this.userMsgBgColor,
    this.decoration,
  })  : isSupportChoose = false,
        messageSelected = false,
        onTapChoose = null;

  const BaseAskMsgCard.edit({
    Key? key,
    required this.role,
    this.child,
    this.botMsgBgColor,
    this.userMsgBgColor,
    this.decoration,
    required bool messageSelected,
    required this.onTapChoose,
  })  : isSupportChoose = true,
        messageSelected = messageSelected,
        super(key: key);

  @override
  State<BaseAskMsgCard> createState() => _BaseAskMsgCardState();
}

class _BaseAskMsgCardState extends State<BaseAskMsgCard> {
  @override
  Widget build(BuildContext context) {
    // 是否支持可选择聊天记录
    if (widget.isSupportChoose != null && widget.isSupportChoose == true) {
      if (widget.role == 0) {
        return _userMessageChoseCard(context);
      } else {
        return _botMessageChoseCard(context);
      }
    } else {
      if (widget.role == 0) {
        return _userMessageCard(context);
      } else {
        return _botMessageCard(context);
      }
    }
  }

  //正常模式
  Widget _userMessageCard(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Flexible(
          child: Container(
            margin: EdgeInsets.only(
                left: getCompatDeviceMargin(context),
                right: 4,
                top: 8,
                bottom: 8),
            decoration: BoxDecoration(
              color: widget.userMsgBgColor ?? _defaultUserMsgColor,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(16),
                  topRight: Radius.circular(16),
                  bottomLeft: Radius.circular(16),
                  bottomRight: Radius.circular(4)),
            ),
            child: widget.child,
          ),
        ),
      ],
    );
  }

  Widget _botMessageCard(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Flexible(
          child: Container(
            margin: EdgeInsets.only(
                right: getCompatDeviceMargin(context),
                top: 8,
                bottom: 8),
            decoration: widget.decoration ??
                BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(16),
                        topRight: Radius.circular(16),
                        bottomLeft: Radius.circular(4),
                        bottomRight: Radius.circular(16)),
                    color: widget.botMsgBgColor ?? _defaultBotMsgColor),
            child: widget.child,
          ),
        )
      ],
    );
  }

  //可以勾选的模式
  Widget _userMessageChoseCard(BuildContext context) {
    return Stack(
      children: [
        Container(
          padding: EdgeInsets.only(left: 16),
          child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  margin: EdgeInsets.only(right: 20),
                  width: 20, // 图片宽18，右10
                  child: Image.asset(
                    (widget.messageSelected ?? false
                        ? ImageConst.ICON_HAS_SELECTED
                        : ImageConst.ICON_UNSELECTED),
                    height: 20,
                  ),
                ),
                Flexible(
                  child: Container(
                    margin: EdgeInsets.only(
                        left: getCompatDeviceMargin(context),
                        right: 16,
                        top: 8,
                        bottom: 8),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(16),
                          topRight: Radius.circular(16),
                          bottomLeft: Radius.circular(16),
                          bottomRight: Radius.circular(4)),
                      color: widget.userMsgBgColor ?? _defaultUserMsgColor,
                    ),
                    child: this.widget.child,
                  ),
                ),
              ]),
        ),
        Positioned.fill(
          child: GestureDetector(
            onTap: widget.onTapChoose,
            behavior: HitTestBehavior.opaque,
            child: Container(color: Colors.transparent),
          ),
        ),
      ],
    );
  }

  Widget _botMessageChoseCard(BuildContext context) {
    return Stack(children: [
      Container(
        padding: EdgeInsets.only(left: 16),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              margin: EdgeInsets.only(right: 20),
              width: 20,
              child: Image.asset(
                (widget.messageSelected ?? false
                    ? ImageConst.ICON_HAS_SELECTED
                    : ImageConst.ICON_UNSELECTED),
                height: 20,
              ),
            ),
            Flexible(
              child: Container(
                margin: EdgeInsets.only(
                    left: 16,
                    right: getCompatDeviceMargin(context),
                    top: 8,
                    bottom: 8),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(16),
                        topRight: Radius.circular(16),
                        bottomLeft: Radius.circular(4),
                        bottomRight: Radius.circular(16)),
                    color: widget.botMsgBgColor ?? _defaultBotMsgColor),
                child: this.widget.child,
              ),
            ),
          ],
        ),
      ),
      Positioned.fill(
        child: GestureDetector(
          onTap: widget.onTapChoose,
          behavior: HitTestBehavior.opaque,
          child: Container(color: Colors.transparent),
        ),
      ),
    ]);
  }
}

///兼容pad和phone设备的距离屏幕最左边或者最右边的间距
double getCompatDeviceMargin(BuildContext context) {
  double width = MediaQuery.of(context).size.width;
  double margin;
  if (width >= 1000) {
    margin = 256;
  } else if (width >= 550) {
    margin = 84;
  } else {
    margin = 16;
  }
  return margin;
}
